import java.util.ArrayList;
import java.util.Collections;

public class DailySchedule
{
   public DailySchedule()
   {
      daySchedule = new ArrayList<Request>();
   }

   public void add(Request r)
   {
      if(!this.check(r))
         throw new IllegalArgumentException("Cannot add event. Time conflict occured.");
      else
      {
         daySchedule.add(r);
      }
      Collections.sort(daySchedule);
      
      
      
   }
   public void cancel(Request r)
   {
      for(int i=0;i<daySchedule.size();i++)
      {
         if(r.getStartTime()==daySchedule.get(i).getStartTime()
               && r.getDay().equalsIgnoreCase(daySchedule.get(i).getDay()))
         {
            daySchedule.remove(i);
         }
      }
   }
   public boolean check(Request r)
   {
         for(int i=0;i<daySchedule.size();i++)
         {
            if(r.getStartTime().isMorning())
            {
               if(r.getStartTime().compareTo(daySchedule.get(i).getEndTime())>=0)
               {
                  return true;
               }
            }
            else if(r.getStartTime().compareTo(daySchedule.get(i).getEndTime())<=0)
            {
               return false;
            }
            if(r.getEndTime().isMorning())
            {
               if(r.getEndTime().compareTo(daySchedule.get(i).getStartTime())<=0)
               {
                  return true;
               }
            }
            else if(r.getEndTime().compareTo(daySchedule.get(i).getStartTime())>=0)
            {
               return false;
            }
         }
      return true;
   }
    
   public String toString()
   {
      String result = "";
      for(int i=0;i<daySchedule.size();i++)
      {
         result+= daySchedule.get(i).toString() + "\n";
      }
      return result;
   }
   private ArrayList<Request> daySchedule;
}
